/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package domain;
import java.util.Vector;

import domain.Game.action;
import domain.exceptions.CannotMoveThereException;
import domain.exceptions.NoSuchSquareException;
import domain.exceptions.TryingToRemoveNonExistingFlagException;
/**
 *
 * @author shaigi
 */
public class AgentDumbAuto extends Agent implements Cloneable{
    
    
//    public AgentDumbAuto(String name,action lastDirection, int arsenal, String assignment) {
//        super(name, lastDirection, arsenal, assignment);
//    }
    public AgentDumbAuto(String name,action lastDirection, String assignment) {
        super(name, lastDirection, assignment);
    }
    public AgentDumbAuto(AgentDumbAuto other) {
//    	super(new String(other.getName()), other.getLastDirection());
    	super(other);
	}
    
    /*
    @Override
    public void die(Board board) {
        System.err.println(this.toString() + "says: Dutz");
    }
    */
    
    @Override
    public void play(Board board, Vector<Agent> agents, int maxSteps) {
        // search for an obstacle in the destination square:
        if (tryToMakeMove(board, agents)){
        } else {
            changeDirection();
            tryToMakeMove(board, agents);
        }
        shoot(agents, board);
    }

//    /**
//     * 
//     * @param agent to shoot
//     * @return true iff there is a clean shot between this and the agent
//     */
//    private boolean inSight(Agent agent) {
//        try {
//        	//using inSight in the board, that gets as a parameter two squares and return
//        	//true iff there is a they are parallel and have no obsticle between them.
//            return Game._board.inSight(this.getSquare(), agent.getSquare());
//        } catch (MultipleAgentsHaveTheSameLocation ex) {
//            System.err.println(ex.getMessage());
//        }
//        return false;//TODO: CHECK IF NOT TO CHANGE
//    }

    
//    private Vector<Agent> scan(Vector<Agent> agents) {
//
//        //Vector<Agent> agents = Game.getAgents();
//        Vector<Agent> agentsToKill = new Vector<Agent>();
//        for (Iterator<Agent> it = agents.iterator(); it.hasNext();) {
//            Agent agent = it.next();
//            if( agent != this && inSight(agent) ){
//                agentsToKill.add(agent);
//            }
//        }//end of for
//        return agentsToKill;
//    }
    
    private boolean tryToMakeMove(Board board, Vector<Agent> agents){
        action lastAction = getLastAction();
        Square nextSquare = null;
        try {
            nextSquare = getSquare().neighbour(lastAction, board);
        } catch (NoSuchSquareException ex) {
            Debug.println(ex.getMessage());
        }
        if( !( nextSquare instanceof SquareWall || nextSquare.isThereAgentHere())){
            try {
                act(lastAction, board, agents);
            } catch (CannotMoveThereException ex) {
                Debug.println(ex.getMessage());
                return false;
            }
            if (nextSquare.isContainFlag()){
                try {
                    takeFlag(nextSquare, board);
                } catch (TryingToRemoveNonExistingFlagException ex) {
                    System.err.println("Flag doesn't exist here: " + getSquare() );
                }
            }
            return true;
        }
        return false;
    }

    private void changeDirection() {
        if (getLastAction() == action.RIGHT) {
            setLastAction(action.LEFT);
        } else {
            setLastAction(action.RIGHT);
        }
    }
//
//    protected void shoot(Vector<Agent> agents, Board board){
//        Vector<Agent> agentsToShoot = scan(agents);
//        for (Iterator<Agent> it = agentsToShoot.iterator(); it.hasNext();) {
//            it.next().die(board);//TODO: check here if we do not miss the first agent.
//        }
//    }
    
//    @Override
//    public Object clone(){
//    	AgentDumbAuto a = (AgentDumbAuto)super.clone();
//    	System.err.println("AgentDumb CLONED");
//    	return new AgentDumbAuto(this);
//    }
    
    @Override
    public boolean equals(Object other){
    	if(!(other instanceof AgentDumbAuto)){
    		return false;
    	}
    	else{
    		return super.equals(other);
    	}
    }
 
}
